package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.bean.MyFrameBean;
import flc.ast.databinding.ActivityPlacardBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.FastClickUtil;
import stark.common.basic.utils.RxUtil;

public class PlacardActivity extends BaseAc<ActivityPlacardBinding> {

    public static MyFrameBean myFrameBean;
    public static int selPosition;
    public static String imgPath;
    private Dialog myTipDialog;

    @Override
    protected int onCreate() {
        return R.layout.activity_placard;
    }

    @Override
    public void onBackPressed() {
        myTipDialog.show();
    }

    @Override
    protected void initView() {
        mDataBinding.ivPlacardBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivPlacardSave.setOnClickListener(this);
        if (selPosition==0){
            //设置图片
            Glide.with(mContext).load(imgPath).transform(new RoundedCorners(200)).into(mDataBinding.myPhoto);
        }else if (selPosition==1||selPosition==2||selPosition==4){
            Glide.with(mContext).load(myFrameBean.getImg2()).into(mDataBinding.myPhoto);
        }else {
            Glide.with(mContext).load(imgPath).into(mDataBinding.myPhoto);
        }
        //提示弹框
        tipDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        if (FastClickUtil.isFastClick()) return;
        switch (v.getId()){
            case R.id.ivPlacardSave:
                saveImg();
                break;
            //提示弹框
            case R.id.ivDialogTipCancel://取消
                myTipDialog.dismiss();
                break;
            case R.id.ivDialogTipRight://确定
                myTipDialog.dismiss();
                finish();
                break;
        }
    }

    @Override
    protected void initData() {
        if (selPosition==0){
            mDataBinding.tvPlacardTitle.setText(R.string.placard_style1);
        }else if (selPosition==1){
            mDataBinding.tvPlacardTitle.setText(R.string.placard_style2);
        }else if (selPosition==2){
            mDataBinding.tvPlacardTitle.setText(R.string.placard_style3);
        }else if (selPosition==3){
            mDataBinding.tvPlacardTitle.setText(R.string.placard_style4);
        }else if (selPosition==4){
            mDataBinding.tvPlacardTitle.setText(R.string.placard_style5);
        }else if (selPosition==5){
            mDataBinding.tvPlacardTitle.setText(R.string.placard_style6);
        }else if (selPosition==6){
            mDataBinding.tvPlacardTitle.setText(R.string.placard_style7);
        }
        showDialog(getString(R.string.set_placard_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dismissDialog();
                //设置内部图片位置
                setPicPosition(myFrameBean);
            }
        }, 1000);
    }

    //设置内部图片位置
    private void setPicPosition(MyFrameBean bean) {
        RectF destRect = new RectF();
        destRect.top = bean.getImageTop();
        destRect.bottom = bean.getImageBottom();
        destRect.right = bean.getImageRight();
        destRect.left = bean.getImageLeft();
        if (selPosition==1||selPosition==2||selPosition==4){
            mDataBinding.myPhoto.setPhotoFrame(ImageUtils.getBitmap(imgPath), bean.getImageWidth(),
                    bean.getImageHeight(), destRect, 0);
        }else if (selPosition==3){
            mDataBinding.myPhoto.setPhotoFrame(ImageUtils.getBitmap(bean.getImg2()), bean.getImageWidth(),
                    bean.getImageHeight(), destRect, -4);
        }else {
            mDataBinding.myPhoto.setPhotoFrame(ImageUtils.getBitmap(bean.getImg2()), bean.getImageWidth(),
                    bean.getImageHeight(), destRect, 0);
        }

    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.save_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<Boolean>() {

                    @Override
                    public void doBackground(ObservableEmitter<Boolean> emitter) {
                        //保存图片
                        ImageUtils.save2Album(mDataBinding.myPhoto.getRetBitmap(), Bitmap.CompressFormat.PNG);
                        emitter.onNext(true);
                    }

                    @Override
                    public void accept(Boolean isSave) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_suc);
                        startActivity(HomeActivity.class);
                    }
                });
            }
        }, 1000);
    }

    //提示弹框
    private void tipDialog() {
        myTipDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_tip, null);
        myTipDialog.setContentView(inflate);
        myTipDialog.setCancelable(true);
        Window dialogWindow = myTipDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        lp.y = 50;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView cancel = inflate.findViewById(R.id.ivDialogTipCancel);
        ImageView right = inflate.findViewById(R.id.ivDialogTipRight);
        TextView textView=inflate.findViewById(R.id.tvDialogTipText);
        textView.setText(getString(R.string.image_not_save));
        cancel.setOnClickListener(this);
        right.setOnClickListener(this);
    }

}